package com.kcnet.qk.controller.hdyh;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.app.AppContext;
import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import static com.kcnet.qk.util.NodeUtil.findNodeByTextAndPerformClick;

/**
 * Created by Administrator on 2018/5/3 0003.
 */

public class HdyhNotiyCTR extends BasePeopleController {

    public boolean init_imm;

    private static String TAG = "养号";

    private int maxAddCount = 10; //最大添加10人
    private int currentAddCount = 0; //当前任务添加人数

    private static HdyhNotiyCTR mRongCloudInstance;


    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static HdyhNotiyCTR getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (HdyhNotiyCTR.class) {
                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new HdyhNotiyCTR();
                }
            }
        }

        return mRongCloudInstance;
    }

    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();

        if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            init_imm =true;
            tagTextClick("通知");
        } else if (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView")) {
            begin();
        } else if(init_imm && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED){

            friendHomeGotoBack();
        }
    }

    public void friendHomeGotoBack() {
        sleepTime(3500);
        AccessibilityNodeInfo root = getRoot();
        List<AccessibilityNodeInfo> list = root.findAccessibilityNodeInfosByText("返回");
        if (!list.isEmpty()) {
            NodeUtil.performClick(list.get(0));
            Log.d("ND", "点击返回 回到好友主页 并且再次回退");
        }
    }
    /**
     * 執行添加推薦的好友
     */
    private void begin() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, "android.support.v7.widget.RecyclerView");
                Log.d("ND", "scrllorNodeInfos" + scrllorNodeInfos.size());

                if (scrllorNodeInfos.size() == 2) {
                    scrllorNodeInfo = scrllorNodeInfos.get(1);
                }else if(scrllorNodeInfos.size() >2){
                    scrllorNodeInfo = scrllorNodeInfos.get(2);
                }
                if (scrllorNodeInfo == null) {
                   // ToastUtil.showToast(AppContext.getInstance().getContext(), "scrllorNodeInfo listView null");
                    return;
                }

                for (int q = 0; q < scrllorNodeInfo.getChildCount(); q++) {

                    if (currentAddCount == maxAddCount) {
                        currentAddCount = 0;
                        // friendHomeGotoBack();
                        stopTask();
                        //停止任务
                        break;
                    }
                    sleepTimedDefault();
                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(q);
                    Log.d("ND", "as " + as.getClassName());
                    currentAddCount++;
                    Log.d("ND", "点击了点赞按钮" +  tagTextClickShell(as));

                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }

            }
        }
    }

    public void setParms(int maxAddCount,int minAddTime,int maxAddTime){
        //stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
    }



}
